package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.UserFollow;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserFollowService;
import com.atguigu.vo.UserFollowVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @PACKAGE_NAME: com.atguigu.controller
 * @CLASSNAME: UserFollowController
 * @AUTHOR: zhangsan
 * @DATE: 2024/4/29 15:51
 * @SINCE 17.0.7
 * @DESCRIPTION: UserFollowController
 */
@RestController
@RequestMapping("/userFollow")
public class UserFollowController {
    @Reference
    private UserFollowService userFollowService;

    /**
     * 前提是已经登录了
     *
     * @return
     */
    @RequestMapping("/auth/list/{pageNum}/{pageSize}")
    public Result findListPage(@PathVariable("pageNum") Integer pageNum,
                               @PathVariable("pageSize") Integer pageSize,
                               HttpSession session) {
        //从session中拿到用户信息
        Object userInfo = session.getAttribute("USER");
        if (userInfo == null) {
            return Result.build("未登录", ResultCodeEnum.LOGIN_AUTH);
        }
        Long id = ((UserInfo) userInfo).getId();
        PageInfo<UserFollowVo> pageInfo = userFollowService.findListPage(pageNum, pageSize, id);
        return Result.ok(pageInfo);
    }

    @RequestMapping("/auth/follow/{houseId}")
    public Result follow(@PathVariable("houseId") Long houseId, HttpSession session) {
        Object userInfo = session.getAttribute("USER");
        if (null == userInfo) {
            return Result.build("未登录", ResultCodeEnum.LOGIN_AUTH);
        }
        UserFollow userFollow = userFollowService.follow(((UserInfo) userInfo).getId(), houseId);
        return Result.ok(userFollow);
    }


    @GetMapping("/auth/cancelFollow/{id}")
    public Result cancelFollow(@PathVariable("id") Long id) {
        userFollowService.cancelFollow(id);
        return Result.ok();
    }

    @GetMapping("/auth/cancelFollow2/{userId}/{houseId}")
    public Result cancelFollow2(@PathVariable("userId") Long userId,
                                @PathVariable("houseId") Long houseId) {
        userFollowService.cancelFollow(userId, houseId);
        return Result.ok();
    }


}
